package com.ayoka.parenting.http;

import android.text.TextUtils;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.R;
import com.ayoka.parenting.base.AppContext;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.bean.WeiXinLoginGetAccessTokenBean;
import com.ayoka.parenting.bean.WeiXinLoginGetUserInfoBean;
import com.jincp.common.http.HttpBeanCallback;
import com.jincp.common.http.HttpProcessor;
import com.jincp.common.utils.MyLogger;
import com.jincp.common.utils.UIUtils;

/**
 * =========================================================
 * @ProjectName:
 * @ClassName: HttpBeanInterceptCallback
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/10/11 14:20
 * =========================================================
 */
public abstract class HttpBeanInterceptCallback<T> extends HttpBeanCallback<T> {

    @Override
    public void onSuccess(int code, T response) {

        /**
         * 根据需要重新定制成功失败判断
         */
        if (response instanceof BaseResponse) {
            // 处理用户相关返回体
            BaseResponse responseBase = (BaseResponse) response;
            if (0 == responseBase.errcode) {
                onSuccess(response);
            } else {
                onFailure(responseBase.errcode, responseBase.errmsg);
            }
        } else if (response instanceof WeiXinLoginGetAccessTokenBean) {
            // 处理用户相关返回体
            onSuccess(response);
        } else if (response instanceof WeiXinLoginGetUserInfoBean) {
            // 处理用户相关返回体
            onSuccess(response);
        } else {
            onFailure(HttpProcessor.ERROR_FROM_SERVER, "服务器错误！");
        }
    }

    public abstract void onSuccess(T response);

    public abstract void onFailure(int errorCode, String errorMessage);

    @Override
    public void onResponseFailure(int errorCode, String errorMessage) {
        if (errorCode == Constants.WX_ERROR_TOKEN) {
            refreshToken();
            return;
        } else if (errorCode == Constants.WX_ERROR_CODE) {
            AppContext.reLogin();
            return;
        }
        if (errorCode == HttpProcessor.ERROR_FROM_SERVER) {
            onFailure(errorCode, UIUtils.getString(R.string.service_error));
        } else if (errorCode == HttpProcessor.ERROR_FROM_NETWORK) {
            onFailure(errorCode, UIUtils.getString(R.string.network_error));
        } else if (errorCode == HttpProcessor.ERROR_FROM_PARSER) {
            onFailure(errorCode, UIUtils.getString(R.string.parse_error));
        } else {
            onFailure(errorCode, errorMessage);
        }
    }

    // 刷新Token
    public synchronized void refreshToken() {
        if (UserManager.getInstance() == null ||
                TextUtils.isEmpty(UserManager.getInstance().refresh_token)) {
            AppContext.reLogin();
            return;
        }
        MyLogger.d("refreshToken");
        API.refreshToken(null, UserManager.getInstance().uid, UserManager.getInstance().refresh_token, new HttpBeanInterceptCallback<BaseResponse<UserManager.TokenBean>>() {

            @Override
            public void onSuccess(BaseResponse<UserManager.TokenBean> response) {
                if (response.data == null)
                    return;
                UserManager.getInstance().auth_token = response.data.auth_token;
                UserManager.getInstance().refresh_token = response.data.refresh_token;
                UserManager.save();
                MyLogger.d("OkHttpListener", "刷新Token成功");
                HttpBeanInterceptCallback.this.onFailure(Constants.WX_ERROR_TOKEN, "刷新Token成功");
            }

            @Override
            public void onFailure(int code, String errorMessage) {
                AppContext.reLogin();
            }
        });
    }
}
